﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deeper.Common;
using Deeper.Common.Web;
namespace Deeper.Controls
{
	[Serializable]
	[ToolboxData("<{0}:PagedReapter runat=server></{0}:PagedReapter>")]
	public class PagedReapter : Repeater, IPostBackEventHandler
	{
		[Description("Postback事件")]
		public void RaisePostBackEvent(String eventArgument)
		{
			int intNewPage = 0;
			switch (eventArgument)
			{

				case "first":
					intNewPage = 1;
					break;
				case "last":
					intNewPage = PageCount;
					break;
				case "next":
					intNewPage = PageIndex + 1;
					if (intNewPage > PageCount) intNewPage = PageCount;
					break;
				case "pre":
					intNewPage = PageIndex - 1;
					if (intNewPage < 1) intNewPage = 1;
					break;
				default:
					int.TryParse(eventArgument, out intNewPage);
					break;
			}
			if (intNewPage > 0 && intNewPage <= PageCount) PageIndexChanged(intNewPage);
		}


		/// <summary>
		/// 总页数
		/// </summary>
		public int PageCount { get { return RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1; } }


		[Category("data"), Description("每页行数"), Browsable(true), DefaultValue(0)]
		public int PageSize
		{
			get { return ViewState.GetFromViewState<int>("pageSize", 10); }
			set { ViewState["pageSize"] = value; }
		}
		[Description("总记录数"), Browsable(true), DefaultValue(0)]
		public int RecordCount
		{
			get { return ViewState.GetFromViewState<int>("RecordCount", 0); }
			set { ViewState["RecordCount"] = value; }
		}

		[Category("data"), Description("当前页数"), Browsable(false), DefaultValue(1)]
		public int PageIndex
		{
			get { return ViewState.GetFromViewState<int>("PageIndex", 1); }
			set { ViewState["PageIndex"] = value; }
		}

		[Category("data"), Description("空数据是否显示暂无数据"), Browsable(true), DefaultValue(true)]
		public bool IsShowEmpty
		{
			get { return ViewState.GetFromViewState<Boolean>("IsShowEmpty", true); }
			set { ViewState["IsShowEmpty"] = value; }
		}

		public delegate PageResult DataBindEventHandler(int pageindex, int pagesize);
		[Description("数据绑定事件"), Category("Action")]
		public event DataBindEventHandler BindData;




		[Description("数据重新绑定")]
		public void ReBind()
		{
			PageIndexChanged(1);
		}

		[Description("页数变化事件")]
		private void PageIndexChanged(int newIndex)
		{
			PageIndex = newIndex;
			if (BindData != null)
			{
				PageResult pr = BindData(PageIndex, PageSize);
				this.RecordCount = pr.Count;
				this.DataSource = pr.Value;
				this.DataBind();
			}
		}

		[Description("手动绑定指定数据")]
		public void PagedReBind(Func<PageResult> fun)
		{
			PageResult pr = fun();
			this.RecordCount = pr.Count;
			this.DataSource = pr.Value;
			this.DataBind();
		}

		[Description("页面呈现方式")]
		protected override void Render(HtmlTextWriter writer)
		{

			base.Render(writer);
			if (IsShowEmpty && RecordCount <= 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "EmptyData");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				writer.Write("暂无数据");
				writer.RenderEndTag();
			}
			if (RecordCount > 0 || DesignMode)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "page");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);

				if (PageIndex > 1 || DesignMode)
				{
					string FirstHref = Page.ClientScript.GetPostBackClientHyperlink(this, "first");
					writer.MakeHypeLink("", FirstHref, "第一页");
					string PrfHref = Page.ClientScript.GetPostBackClientHyperlink(this, "pre");
					writer.MakeHypeLink("", PrfHref, "<<");
				}

				if (PageCount > 1)
				{
					PageNumber page = new PageNumber(PageCount, PageIndex);
					for (int i = page.sIndex; i <= page.eIndex; i++)
					{
						string linHref = Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString());
						writer.MakeHypeLink("", linHref, i == PageIndex ? "[" + i + "]" : i.ToString());
					}
				}

				if (PageIndex < PageCount || DesignMode)
				{
					string NextHref = Page.ClientScript.GetPostBackClientHyperlink(this, "next");
					writer.MakeHypeLink("", NextHref, ">>");
					string LastHref = Page.ClientScript.GetPostBackClientHyperlink(this, "last");
					writer.MakeHypeLink("", LastHref, "最后一页");
				}
				string str = string.Format("共{0}页&nbsp;&nbsp;&nbsp;每页{1}行&nbsp;&nbsp;&nbsp;共{2}条记录", this.PageCount, this.PageSize, this.RecordCount);
				writer.RenderBeginTag(HtmlTextWriterTag.Span);
				writer.Write(str);
				writer.RenderEndTag();
				writer.RenderEndTag();
			}
		}
	}
}
